package drawing 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Stage;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author Ivan
	 */
	public class SpriteBatch 
	{
		//视口
		private var __viewPort:Rectangle;
		
		//绘图板
		private var __canvas:BitmapData;
		
		//绘图板容器
		private var __wrapper:Bitmap; 
		
		public function SpriteBatch(stage:Stage, viewPort:Rectangle) 
		{
			//将绘图板容器添加到舞台
			__wrapper = new Bitmap();
			stage.addChild(__wrapper);
			stage.setChildIndex(__wrapper, 0);
			
			setViewPort(viewPort);
		}
		
		public function begin():void 
		{
			__canvas.lock();
		}
		
		public function end():void 
		{
			__canvas.unlock();
		}
		
		public function copyPixels(bmpd:BitmapData, position:Point):void 
		{
			__canvas.copyPixels(bmpd, bmpd.rect, position);
		}
		
		
		/**
		 * add a sprite to the batch  of sprites to be rendered, specifying the texture, position and source rectangle.
		 * @param	texture
		 * @param	position
		 * @param	sourceRectangle
		 * @param	rotation
		 * @param	scale
		 */
		public function draw(texture:LTexture2D, position:Point, sourceRectangle:Rectangle = null, rotation:Number = 0, scale:Number = 0):void 
		{
			
		}
		
		/**
		 * Clears the viewport to a specified color 
		 * @param	color
		 */
		public function clear(color:uint = 0):void 
		{
			__canvas.fillRect(__canvas.rect, color);
		}
		
		//设置视口
		public function setViewPort(port:Rectangle):void 
		{
			if (__canvas != null)
				__canvas.dispose();
			__canvas = new BitmapData(port.width, port.height, true, 0);
			__wrapper.bitmapData = __canvas;
			
			__wrapper.x = port.x;
			__wrapper.y = port.y;
			__viewPort  = port;  
		}
		
		
	}

}